// pages/distribution/distribution_center.js
const { shopfinance } = require("../../request/api");
const api = require("../../request/api")
const request = require("../../request/request")
const util = require("../../utils/util")
const App = getApp();
Page({
    /**
     * 页面的初始数据
     */
    data: {
        uid: '',
        avatarUrl: 'https://thirdwx.qlogo.cn/mmopen/vi_32/POgEwh4mIHO4nibH0KlMECNjjGxQUq24ZEaGT4poC6icRiccVGKSyXwibcPq4BWmiaIGuG1icwxaQX6grC9VemZoJ8rg/132',
        nickName: '点击显示微信头像',
        if_register: false,
        if_show_mask: false,
        if_show_shop_code: false,
        if_show_paying_code: false,
        if_show_check_code: false,
        myacode: '',
        paying_code: '',
        checkcode_value: '',
        shopfinance_data: {},
        shopdetail_data: {},
        shop_id: 0,
        distribution_icon_list: [
            {
                id: 3,
                title: '商品订单',
                icon: '/images/detail_list_blue.png',
                num: 0.00,
                targetUrl: '/pages/shop/order'
            },

            {
                id: 5,
                title: '商家二维码',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/blank/blank?index=5'
            },
            {
                id: 6,
                title: '我的客户',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/shop/client?index=5'
            },
            {
                id: 7,
                title: '门店收入',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/shop/myincome?index=5'
            },
            {
                id: 8,
                title: '收入明细',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/shop/myincome_log'
            },
            {
                id: 9,
                title: '购买积分',
                icon: '/images/team_blue.png',
                num: '',
                targetUrl: '/pages/shop/buy_point'
            },
            {
                id: 10,
                title: '转增积分',
                icon: '/images/team_blue.png',
                num: 0,
                targetUrl: '/pages/shop/send_point',
            },
            {
                id: 11,
                title: '商家付款码',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/blank/blank?index=5'
            },

            {
                id: 12,
                title: '我的店员',
                icon: '/images/acode_orange.png',
                num: '',
                targetUrl: '/pages/shop/shop_member'
            },
        ],
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        this.init_data(options)
        this.getmyacode()
        this.get_paying_code()
        this.get_shopfinance()
        this.get_shopdetail()
    },
// 选择头像
onChooseAvatar(e){
  const url = e.detail.avatarUrl;
  console.log(url)
  this.setData({avatarUrl: url,});
  wx.uploadFile({
    url: 'ttps://api.gangjinhui.cn/ashop102/uploadfile', //你的上传接口地址
    filePath: url,
    name: 'imgFile', //必填，文件对应的 key , 开发者在服务器端通过这个 key 可以获取到文件二进制内容
    formData: {
      'user': '1' // 这里的user是自定义的字段，test是传入的值
    },
    success: function(uploadFileRes) {
      console.log(uploadFileRes.data)
    }
  })
}, 
  // 头像、昵称同时输入完后才保存，保存成功再关闭
  handleInputAvatarNameAfter(){

  },
    get_shopdetail() {
        let params = {
            id: this.data.eshop_id,
        }
        request.post(api.shopdetail, params).then(res => {
            this.setData({
                shopdetail_data: res,
            })
        })
    },

    get_shopfinance() {
        let params = {
            id: this.data.eshop_id
        }
        request.post(api.shopfinance, params).then(res => {
            this.setData({
                shopfinance_data: res
            })
        })
    },

    getmyacode() {
        let uid = this.data.uid
        let ctype = 6
        let is_qrcode = 1

        let params = {
            uid,
            ctype,
            is_qrcode
        }
        request.post(api.qr_code, params).then(res => {
            this.setData({
                myacode: res.img
            })
        })
    },

    get_paying_code() {
        let uid = this.data.uid
        let ctype = 4
        let is_qrcode = 1

        let params = {
            uid,
            ctype,
            is_qrcode
        }
        request.post(api.qr_code, params).then(res => {
            this.setData({
                paying_code: res.img
            })
        })
    },

    init_data(options) {
        let uid = util.getAppValue(App.globalData.uid)
        let eshop_id = App.globalData.eshop_id
        if (!uid) {
            uid = 1
        }
        this.setData({
            if_register: App.globalData.if_register,
            uid,
            options,
            eshop_id,
        })
        let avatarUrl = App.globalData.userInfo.avatarUrl
        let nickName = App.globalData.userInfo.nickName

        if (avatarUrl) {
            this.setData({
                avatarUrl,
                nickName
            })
        }
    },

    show_shop_code() {
        this.setData({
            if_show_mask: true,
            if_show_shop_code: true,
        })
    },

    show_paying_code() {
        this.setData({
            if_show_mask: true,
            if_show_paying_code: true,
        })
    },

    show_check_code() {
        this.setData({
            if_show_mask: true,
            if_show_check_code: true,
        })
    },

    input_checkcode(e) {
        this.setData({
            checkcode_value: e.detail.value
        })
    },

    check_code_confirm() {
        let params = {
            checkcode: this.data.checkcode_value
        }
        request.post(api.orderconfirm, params).then(res => {
            console.log(res);
            this.setData({
                if_show_mask: false,
                if_show_check_code: false,
            })
            wx.navigateTo({
                url: '/pages/shop/order?index=4',
            })
        })
    },

    check_register() {
        if (!this.data.if_register) {
            this.get_user_profile()
        }
    },

    go_distribution_url(e) {
        let url = e.currentTarget.dataset.url
        if (e.currentTarget.dataset.id == 8) {
            url = url + '?is_vip=' + this.data.shopdetail_data.is_vip
        }
        if (e.currentTarget.dataset.id == 3) {
            url = url + '?shop_id=' + this.data.shopdetail_data.id
        }
        wx.navigateTo({
            url,
        })
    },

    clear_mask() {
        this.setData({
            if_show_mask: false,
            if_show_check_code: false,
            if_show_shop_code: false,
            if_show_paying_code: false,
        })
    },

    get_user_profile() {
        wx.getUserProfile({
            desc: '展示用户信息', // 声明获取用户个人信息后的用途，后续会展示在弹窗中，请谨慎填写
            success: res => {
                App.globalData.userInfo = res.userInfo
                App.globalData.if_register = true
                this.init_data(this.data.options)
                // let pages = getCurrentPages()
                // let prev_page = pages[pages.length - 2]
                // prev_page.onLoad()
                // util.goBack()
            },
            fail: res => {
                console.log(res);
            }
        })
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})